package cn.zh.stan.activity;

import android.app.Dialog;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.R;
import cn.zh.stan.util.Loading;
import cn.zh.stan.util.RegexUtils;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Request;
import okhttp3.Response;

public class ReActivity extends BaseActivity {

    EditText rephone;
    Button next;
    ImageView rpimg;
    LinearLayout rplin;
    ImageView re_back;
    private Dialog LDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_re);
        initre();
        }


    public void initre(){
       rephone= (EditText) findViewById(R.id.rephone);
       next= (Button) findViewById(R.id.next);
       rpimg= (ImageView) findViewById(R.id.rpimg);
       rplin= (LinearLayout) findViewById(R.id.rplin);
       re_back= (ImageView) findViewById(R.id.re_back);
       next.setOnClickListener(this);
       rpimg.setOnClickListener(this);
       re_back.setOnClickListener(this);
       rephone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void afterTextChanged(Editable editable) {
             int len=editable.length();
             if (len>0){
                 rpimg.setVisibility(View.VISIBLE);
                 rplin.setBackgroundResource(R.drawable.cprimbg);
             }else {
                 rpimg.setVisibility(View.INVISIBLE);
                 rplin.setBackgroundResource(R.drawable.graybg);
               }
              }
            });

       }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.next:
                LDialog= Loading.createLoadingDialog(this,"发送中...");
                next.setEnabled(false);
                getPost();
                break;
            case R.id.rpimg:
                rephone.getText().clear();
                rplin.setBackgroundResource(R.drawable.graybg);
                rpimg.setVisibility(View.INVISIBLE);
                break;
            case R.id.re_back:
                finish();
                break;
               }
            }


         public void getPost() {
             final String moblie = rephone.getText().toString();
             boolean ismobile = RegexUtils.checkMobile(moblie);
          if (ismobile){
             OkHttpUtils.post(BaseUrl.pass_msg)
                     .params("moblie", moblie)
                     .execute(new StringCallback() {
                         @Override
                         public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                             try {
                                 JSONObject js = new JSONObject(s);
                                 boolean status = js.getBoolean("State");
                                 Loading.closeDialog(LDialog);
                                 if (status) {
                                     next.setEnabled(true);
                                     boolean Flag = js.getBoolean("Flag");
                                     boolean Mark = js.getBoolean("Mark");
                                     if (Flag) {
                                         Intent intent = new Intent(ReActivity.this, Renext.class);
                                         intent.putExtra("name", moblie);
                                         startActivity(intent);
                                        }
                                     String Msg = js.getString("Msg");
                                     ToastUtil.showShort(ReActivity.this, Msg);

                                 } else {
                                     String msg = js.getString("msg");
                                     ToastUtil.showLong(ReActivity.this, msg);
                                 }
                             } catch (JSONException e) {
                                 e.printStackTrace();
                             }
                         }
                     });
                }else {
              ToastUtil.showLong(ReActivity.this,"手机号错误");
          }

         }




}
